﻿using System.Text;
using System.Text.Json.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace WPFStore
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


    }

    public class ToDoList
    {

        public ToDoList() { }
        public int Id { get; set; }
        public int Name { get; set; }
        public string Description { get; set; }
        public bool IsCompleted { get; set; } = false;
        public bool IsMarked { get; set; } = false;
        public string CreatedAt { get; set; } = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
    }
    public class Product
    {   
        public Product() { }
        private readonly string jsonData = "ToDoList.json";
        public void cmdWriteLine(string message)
        {
            Console.WriteLine(message);
        }
        public List<ToDoList> ToDoLists { get; set; } = new List<ToDoList>();
        public void ShowAllList()
        {
            foreach (var item in ToDoLists)
            {
                cmdWriteLine($"Id: {item.Id}, Name: {item.Name}, Description: {item.Description}, IsCompleted: {item.IsCompleted}, IsMarked: {item.IsMarked}, CreatedAt: {item.CreatedAt}");
            }
        }
        public void AddToDoList(ToDoList todo)
        {
            ToDoLists.Add(todo);
            cmdWriteLine($"Added ToDo: {todo.Name}");
        }
        public void MarkToDoList(int id)
        {
            var todo = ToDoLists.FirstOrDefault(t => t.Id == id);
            if (todo != null)
            {
                todo.IsMarked = true;
                cmdWriteLine($"Marked ToDo: {todo.Name}");
            }
            else
            {
                cmdWriteLine($"ToDo with Id {id} not found.");
            }
        }
        public void CompleteToDoList(int id)
        {
            var todo = ToDoLists.FirstOrDefault(t => t.Id == id);
            if (todo != null)
            {
                todo.IsCompleted = true;
            }
            else
            {
                cmdWriteLine("Cannot find such TODO id");
            }
        }
        public void StoreAllList()
        {
           foreach(var item in ToDoLists)
            {
                using (var file = File.OpenWrite(jsonData))
                {
                    file.Write(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(item)));
                }
            }
        }
        public void GetAllList()
        {
            using(var file = File.OpenRead(jsonData))
            {
                using (var reader  = new StreamReader(file))
                {
                    string json = reader.ReadToEnd();
                    ToDoLists = JsonSerializer.Deserialize<List<ToDoList>>(json) ?? new List<ToDoList>();
                    Dictionary<char, int> dic = new Dictionary<char, int>();
                    dic.Add
                    var d = new Dictionary<char, int>(dic);
                }
            }
        }
    }
}
